package com.zyr.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyr.domain.Order;
import com.zyr.domain.User;
import com.zyr.feign.OrderFeignClient;
import com.zyr.model.Result;
import com.zyr.service.UserService;
import com.zyr.mapper.UserMapper;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 35747
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-07-28 19:06:50
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private OrderFeignClient orderFeignClient;

    @Override
    public Map<String, List<Order>> listAndOrder() {
        HashMap<String, List<Order>> map = new HashMap<>();
        List<User> users = userMapper.selectList(null);
        if (!users.isEmpty()) {
            users.stream().map(User::getId).forEach(u -> {
                // 该userid对应的订单
                List<Order> orders = orderFeignClient.getListByUserId(u);
                map.put(u.toString(), orders);
            });
        }
        return map;
    }

    @Override
    @GlobalTransactional
    public boolean saveit(User user) {
        userMapper.insert(user);
        Order order = new Order();
        order.setOrderName("订单" + user.getUsername());
        order.setOrderDescrition("订单描述" + user.getUsername());
        // 远程调用
        return orderFeignClient.add(order);
    }
}




